package com.yxs.sjbapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
import android.content.Intent;
import android.net.Uri;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class RowActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_row); // 设置对应的布局文件
        
        // 初始化按钮并设置点击监听器
        Button btnGo = findViewById(R.id.go);
        Button btnGpwy = findViewById(R.id.gpwy);
        Button btnExit = findViewById(R.id.exit);
        
        Ynet.sGRA("http://u.com", null, new Ynet.NetworkCallback() {
                @Override
                public void onSuccess(String response) {
                    Toast.makeText(RowActivity.this, "OK!", Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onError(String errorMessage) {
                    Toast.makeText(RowActivity.this, "NO!", Toast.LENGTH_SHORT).show();
                }
            });    
        
        btnExit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {finish();}
            });
        btnGpwy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {YXSA02();}
            });
        btnGo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {openWebPage("https://www.gfbzb.gov.cn/");}
            });
    }
    
    private void openWebPage(String url) {
        try {
            Uri webpage = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
            // 检查是否有浏览器可以处理此Intent
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            } else {
                Toast.makeText(this, "没有可用的浏览器", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "打开链接时发生错误", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    
    private void YXSA02() {
        CharSequence[] items = {"Bilibili", "website(yeb.7749.org)", "?"};
        AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("选择……")
            .setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dia, int which) {
                    switch (which) {
                        case 0: // 点击了 "Bilibili"
                            // 优先尝试使用 Bilibili 客户端打开
                            try {
                                // 方法1: 使用 Bilibili 的深度链接
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse("bilibili://space/1463042868"));
                                startActivity(intent);
                            } catch (Exception e) {
                                // 如果深度链接失败，尝试使用包名直接打开应用
                                try {
                                    Intent intent = getPackageManager().getLaunchIntentForPackage("tv.danmaku.bili");
                                    if (intent != null) {
                                        // 如果找到了 Bilibili 应用，尝试跳转到用户主页
                                        intent.setData(Uri.parse("bilibili://space/1463042868"));
                                        startActivity(intent);
                                    } else {
                                        // 如果没有安装 Bilibili 应用，则使用网页打开
                                        openWebPage("https://space.bilibili.com/1463042868");
                                    }
                                } catch (Exception e2) {
                                    // 所有方法都失败时，使用网页打开
                                    openWebPage("https://space.bilibili.com/1463042868");
                                }
                            }
                            break;
                        case 1: // 点击了 "website(yeb.7749.org)"
                            openWebPage("http://yeb.7749.org");
                            break;
                        case 2: // 点击了 "?"
                            Toast.makeText(RowActivity.this, "敬待更新……?", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            })
            .setNegativeButton(android.R.string.cancel, null)
            .create();
        dialog.show();
    }
}
